package org.ssp.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.ssp.base.TimerMessageThread;
import org.ssp.dao.AssistDao;
import org.ssp.dao.BackupDao;
import org.ssp.dao.TravelDao;
import org.ssp.dao.UserDao;
import org.ssp.impl.AssistImpl;
import org.ssp.impl.BackupImpl;
import org.ssp.impl.TravelImpl;
import org.ssp.impl.UserImpl;
import org.ssp.pojo.Assister;
import org.ssp.pojo.Backup;
import org.ssp.pojo.Task;
import org.ssp.pojo.TimerMessage;
import org.ssp.pojo.Travel;
import org.ssp.pojo.User;
import org.ssp.util.CommonUtil;
import org.ssp.util.SendMail;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * 会员相关的处理
 */
@WebServlet("/member")
public class MemberServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String method=request.getParameter("method");
		/*if(request.getSession().getAttribute("assist")==null){
			response.sendRedirect(request.getContextPath());
			return;
		}*/
		if("page".equals(method)){//1,请求页面
			request.getRequestDispatcher("/WEB-INF/page/admin/members.jsp").forward(request, response);
			
		}else if("total".equals(method)){//2，转载所有会员列表
			System.out.println("member....total......");
			int page=Integer.valueOf(request.getParameter("page"));
			int rows=Integer.valueOf(request.getParameter("rows"));
			int offset=(page-1)*rows;
			int total=0;
			//查出所有用户:  1:total,2 ordinary,3:vip 
			ArrayList<User> list=new ArrayList<User>();
			UserDao userd=new UserImpl();
			list=userd.findAll(offset, rows);
			total=userd.findAll();
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("rows", JSONArray.fromObject(list,jsonConfig));
			json.element("total", total);
			System.out.println(json.toString());
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;

		}else if("vip".equals(method)){//3,装载vip会员列表
			System.out.println("member.....vip......");
			int page=Integer.valueOf(request.getParameter("page"));
			int rows=Integer.valueOf(request.getParameter("rows"));
			int offset=(page-1)*rows;
			int total=0;
			//查出所有用户:  1:total,2 ordinary,3:vip 
			ArrayList<User> list=new ArrayList<User>();
			UserDao userd=new UserImpl();
			list=userd.findVip(offset, rows);
			total=userd.findVip();
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("rows", JSONArray.fromObject(list,jsonConfig));
			json.element("total", total);
			System.out.println(json.toString());
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
			
		}else if("ordinary".equals(method)){//4,装载普通会员列表
			System.out.println("member.....ordinary.....");
			int page=Integer.valueOf(request.getParameter("page"));
			int rows=Integer.valueOf(request.getParameter("rows"));
			int offset=(page-1)*rows;
			int total=0;
			//查出所有用户:  1:total,2 ordinary,3:vip 
			ArrayList<User> list=new ArrayList<User>();
			UserDao userd=new UserImpl();
			list=userd.findOrdinary(offset, rows);
			total=userd.findOrdinary();
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("rows", JSONArray.fromObject(list,jsonConfig));
			json.element("total", total);
			System.out.println(json.toString());
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
			
		}else if("backadd".equals(method)){//5,给用户添加备份信息
			System.out.println("member....backadd....");
			String content=request.getParameter("content");
			String datetime=request.getParameter("datetime");
			String uid=request.getParameter("uid");
			System.out.println(content);
			System.out.println(datetime);
			System.out.println(uid);
			Backup back=new Backup();
			back.setContent(content);
			back.setDatetime(datetime);
			back.setUid(uid);
			BackupDao backd=new BackupImpl();
			backd.addBackup(back);
			JSONObject json=new JSONObject();
			json.element("success", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("backlist".equals(method)){//6,某一用的的备份列表
			System.out.println("member.....backlist....");
			int page=Integer.valueOf(request.getParameter("page"));
			int rows=Integer.valueOf(request.getParameter("rows"));
			System.out.println(page+";...........;"+rows);
			int offset=(page-1)*rows;
			int total=0;
			String uid=request.getParameter("uid");
			ArrayList<Backup> list=new ArrayList<Backup>();
			BackupDao backd=new BackupImpl();
			list=backd.findByUid(uid,offset,rows);
			total=backd.findByUid(uid);
			JSONObject json=new JSONObject();
			json.element("rows", list);
			json.element("total", total);
			System.out.println(json.toString());
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("backdelete".equals(method)){//7，删除备份
			System.out.println("member....delete...");
			String id=request.getParameter("id");
			System.out.println(id);
			BackupDao backd=new BackupImpl();
			backd.deleteBackup(Integer.valueOf(id));
			JSONObject json=new JSONObject();
			json.element("success",true);
			System.out.println(json.toString());
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
		}else if("backedit".equals(method)){//8，编辑备份
			System.out.println("member.......backedit..........");
			String id=request.getParameter("id");
			String content=request.getParameter("content");
			String datetime=request.getParameter("datetime");
			Backup back=new Backup();
			back.setId(Integer.valueOf(id));
			back.setContent(content);
			back.setDatetime(datetime);
			BackupDao backd=new BackupImpl();
			backd.updateBackup(back);
			JSONObject json=new JSONObject();
			json.element("success", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;

		}else if("email".equals(method)){//9，发送邮件
			System.out.println("member....email.....");
			String content=request.getParameter("content");
			String subject=request.getParameter("subject");
			String to=request.getParameter("to");
			System.out.println(content);
			System.out.println(subject);
			System.out.println(to);
			Map<String,String> map=new HashMap<String,String>();
			map.put("smtp", "smtp.qq.com");
			map.put("protocol", "smtp");
			map.put("username", "1255292979");//此处修改为公司的邮箱
			map.put("password","slhphqvgsujkjbgi" );//"slhphqvgsujkjbgi"
			map.put("from", "1255292979@qq.com");
			map.put("to",to);
			map.put("subject", subject);
			map.put("body", content);
	        Map<String,String> image=new HashMap<String,String>();  
	        ArrayList<String> list=new ArrayList<String>();  
	        //从html中分解出image和file
	       Document doc= Jsoup.parse(content);
	        Elements links=doc.getElementsByClass("ke-insertfile");//解析出附件
	        if(links!=null){
	        	for(Element link:links){
		        	list.add(link.attr("href"));
		        }
	        }
	        /* 
	        Elements links2=doc.getElementsByTag("img");
	        if(links!=null){
	        	for(Element link:links2){
		        }
	        }*/
	        
	        SendMail sm=new SendMail(map,list,image);  
	        try {
				sm.send();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	        JSONObject json=new JSONObject();
	        json.element("success", true);
	        PrintWriter out=response.getWriter();
	        out.write(json.toString());
			return;
		
	    }else if("travels".equals(method)){
	    	System.out.println("member...travel....");
	    	String uid=request.getParameter("uid");
	    	int page=Integer.valueOf(request.getParameter("page"));
			int rows=Integer.valueOf(request.getParameter("rows"));
			System.out.println(page+";...........;"+rows);
			int offset=(page-1)*rows;
			int total=0;
			TravelDao traveld=new TravelImpl();
			ArrayList<Travel> list=traveld.findByUid(uid, offset, rows);
			total=traveld.findByUid(uid);
			JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("rows", JSONArray.fromObject(list,jsonConfig));
			json.element("total", total);
			System.out.println(json.toString());
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
			
	    }else if("files".equals(method)){
	    	System.out.println("member....files....");
	    	String uid=request.getParameter("uid");
	    	UserDao ud=new UserImpl();
	    	ArrayList<String[]> list=ud.findFileByUid(uid);
	    	JSONObject json=new JSONObject();
	    	json.element("filelist", list);
	    	PrintWriter out=response.getWriter();
	    	out.write(json.toString());
	    	System.out.println(json.toString());
	    	return;
	    }else if("addfile".equals(method)){//给会员添加附件
	    	System.out.println("task.....addfile");
	    	String uid=request.getParameter("uid");
	    	String url=request.getParameter("url");
	    	String title=request.getParameter("title");
	    	UserDao ud=new UserImpl();
	    	ud.addFile(uid,url,title);
			JSONObject json=new JSONObject();
			json.element("success", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
	    }else if("deletefile".equals(method)){//删除会员附件
	    	System.out.println("member.....deletefile......");
	    	String uid=request.getParameter("uid");
	    	String url=request.getParameter("url");
	    	UserDao ud=new UserImpl();
	    	ud.deleteFile(uid, url);
	    	JSONObject json=new JSONObject();
			json.element("success", true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			return;
	    }else if("tasks".equals(method)){
	    	System.out.println("member...tasks......");
	    	String uid=request.getParameter("uid");
	    	UserDao ud=new UserImpl();
	    	ArrayList<Task> list=ud.findTaskByUid(uid);
	    	JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("success",true);
			json.element("tasklist", JSONArray.fromObject(list, jsonConfig));
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			System.out.println(json.toString());
			return;
	    }else if("taskinfo".equals(method)){//任务详情
	    	System.out.println();
	    	
	    }else if("userlink".equals(method)){
	    	System.out.println("member....userlink.....");
	    	String uid=request.getParameter("uid");
	    	String url=request.getParameter("url");
	    	UserDao ud=new UserImpl();
	    	if(ud.findLinkByUid(uid)!=null){//更新
	    		ud.updateUserLink(uid,url);
	    	}else{//插入
		    	ud.addUserLink(uid, url);
	    	}
	    	JSONObject json=new JSONObject();
	    	json.element("success", true);
	    	PrintWriter out=response.getWriter();
			out.write(json.toString());
			System.out.println(json.toString());
			return;
	    }else if("linkvalue".equals(method)){
	    	System.out.println("member....linkvalue.....");
	    	String uid=request.getParameter("uid");
	    	System.out.println(uid);
	    	UserDao ud=new UserImpl();
	    	String url=ud.findLinkByUid(uid);
	    	JSONObject json=new JSONObject();
	    	if(url!=null){
	    		json.element("success", true);
	    	}else{
	    		json.element("success", false);
	    	}
	    	json.element("url", url);
	    	PrintWriter out=response.getWriter();
			out.write(json.toString());
			System.out.println(json.toString());
			return;
	    }else if("usertravels".equals(method)){//会员参与的活动
	    	System.out.println("member....usertravels.....");
	    	String uid=request.getParameter("uid");
	    	UserDao ud=new UserImpl();
	    	ArrayList<Travel> list=ud.findTravelsByUid(uid);
	    	JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("success",true);
			json.element("travellist", JSONArray.fromObject(list, jsonConfig));
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			System.out.println(json.toString());
			return;
	    }else if("travelinfo".equals(method)){//活动详情
	    	System.out.println("member....travelinfo.....");
	    	String id=request.getParameter("travelid");
	    	UserDao ud=new UserImpl();
	    	Travel travel=ud.findTravelById(Integer.valueOf(id));
	    	JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("success",true);
			json.element("travel", JSONObject.fromObject(travel, jsonConfig));
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			System.out.println(json.toString());
			return;
	    }else if("useredit".equals(method)){//修改会员资料
	    	System.out.println("member.....useredit....");
	    	String uid=request.getParameter("uid");
	    	String name=request.getParameter("name");
	    	String type=request.getParameter("type");
	    	String phone=request.getParameter("phone");
	    	String email=request.getParameter("email");
	    	String school=request.getParameter("school");
	    	String grade=request.getParameter("grade");
	    	String due=request.getParameter("due");
	    	String assistid=request.getParameter("assistid");
	
	    	System.out.println("uid"+uid);
	    	System.out.println("name"+name);
	    	System.out.println("type"+type);
	    	System.out.println("phone"+phone);
	    	System.out.println("email"+email);
	    	System.out.println("school"+school);
	    	System.out.println("grade"+grade);
	    	System.out.println("due"+due);
	    	System.out.println("asistid"+assistid);
	    	User user=new User();
	    	user.setUid(uid);
	    	user.setName(name);
	    	user.setType(Integer.valueOf(type));
	    	user.setPhone(phone);
	    	user.setEmail(email);
	    	user.setSchool(school);
	    	user.setGrade(Integer.valueOf(grade));
//	    	user.setDue(new Date(due.replace("-", "/")));
	    	user.setDue(due);
	    	user.setAssistid(assistid);
	    	UserDao ud=new UserImpl();
	    	ud.updateUserInfo(user);
	    	JSONObject json=new JSONObject();
	    	json.element("success", true);
	    	PrintWriter out=response.getWriter();
	    	out.write(json.toString());
	    	return;
	    	
	    }else if("adduser".equals(method)){//手动添加用户
	    	System.out.println("member....adduser....");
	    	String name=request.getParameter("name");
	    	String type=request.getParameter("type");
	    	String phone=request.getParameter("phone");
	    	String email=request.getParameter("email");
	    	String school=request.getParameter("school");
	    	String grade=request.getParameter("grade");
	    	String due=request.getParameter("due");
	    	String assistid=request.getParameter("assistid");
	    	System.out.println("name"+name);
	    	System.out.println("type"+type);
	    	System.out.println("phone"+phone);
	    	System.out.println("email"+email);
	    	System.out.println("school"+school);
	    	System.out.println("grade"+grade);
	    	System.out.println("due"+due);
	    	System.out.println("asistid"+assistid);
	    	//自动生成uid
	    	String uid=UUID.randomUUID().toString();
	    	System.out.println(uid);
	    	
	    	User user=new User();
	    	user.setUid(uid);
	    	user.setName(name);
	    	user.setType(Integer.valueOf(type));
	    	user.setPhone(phone);
	    	user.setEmail(email);
	    	user.setSchool(school);
            if(grade!=""){
    	    	user.setGrade(Integer.valueOf(grade));
            }else{
            	user.setGrade(0);
            }
	    	if(due!=""){//08/02/2016
	    		user.setDue(new Date(due).toLocaleString());//转换成2016-08-02
	    	}else{
	    		user.setDue(new Date().toLocaleString());
	    	}
	    	user.setAssistid(assistid);
	    	UserDao ud=new UserImpl();
	    	ud.addUserByAdminer(user);
	    	JSONObject json=new JSONObject();
	    	json.element("success", true);
	    	PrintWriter out=response.getWriter();
	    	out.write(json.toString());
	    	return;
	    	
	    }else if("myuser".equals(method)){//助理负责的管理员
	    	System.out.println("member...myuser.....");
	    	Assister assist=(Assister)request.getSession().getAttribute("assist");
	    	int page=Integer.valueOf(request.getParameter("page"));
	    	int rows=Integer.valueOf(request.getParameter("rows"));
	    	int offset=(page-1)*rows;
	    	int total=0;
	    	UserDao userd=new UserImpl();
	    	ArrayList<User> list=userd.findByAssistid(assist.getId(),offset,rows);
	    	total=userd.findByAssistid(assist.getId());
	    	JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			json.element("success",true);
			json.element("rows", JSONArray.fromObject(list, jsonConfig));
			json.element("total", total);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
			System.out.println(json.toString());
			return;
	    	
	    }else if("addcomment".equals(method)){//添加备注
	    	System.out.println("member....addcomment.....");
	    	String content=request.getParameter("content");
	    	String uid=request.getParameter("uid");
	    	String assistid=((Assister)request.getSession().getAttribute("assist")).getId();
	    	TimerMessage tm=new TimerMessage();
	    	tm.setContent(content);
	    	tm.setUid(uid);
	    	tm.setAssistid(assistid);
	    	UserDao ud=new UserImpl();
	    	TimerMessage tmread=ud.addComment(tm);
	    	JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
	    	if(tmread!=null){
	    		json.element("success", true);
	    		json.element("timermessage", JSONObject.fromObject(tmread, jsonConfig));
	    	}else{
	    		json.element("success", false);
	    	}
	    	PrintWriter out=response.getWriter();
	    	out.write(json.toString());
	    	return;
	    }else if("settimer".equals(method)){//设置备注为消息提醒
	    	System.out.println("member....settimer......");
	    	int id=Integer.valueOf(request.getParameter("id"));
	    	String datetime=request.getParameter("datetime");
	    	System.out.println(new Date(datetime));
	    	//开启子线程处理定时任务
	    	TimerMessageThread tm=new TimerMessageThread(id,new Date(datetime));
	    	Thread thread=new Thread(tm);
	    	thread.start();
	    	JSONObject json=new JSONObject();
	    	json.element("success", true);
	    	PrintWriter out=response.getWriter();
	    	out.write(json.toString());
	    	System.out.println(json.toString());
	    	return;
	   
	    }else if("cancelnotidy".equals(method)){//取消备注为消息提醒
	    	System.out.println("member....cancelnotify.....");
	    	
	    }else if("refreshmsg".equals(method)){//前台定时刷新消息
	    	System.out.println("member...refreshmsg.....");
	    	
	    }else if("commentlist".equals(method)){//comments列表
	    	System.out.println("member.....commentlist.....");
	    	String assistid=((Assister)request.getSession().getAttribute("assist")).getId();
	    	String uid=request.getParameter("uid");
	    	System.out.println(uid+"....");
	    	
	    	UserDao ud=new UserImpl();
	    	ArrayList<TimerMessage> list=ud.findCommentByassist(assistid,uid);
	    	JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
	    	json.element("success", true);
	    	json.element("comments", JSONArray.fromObject(list, jsonConfig));
	    	PrintWriter out=response.getWriter();
	    	out.write(json.toString());
	    	System.out.println(json.toString());
	    	return;
	    	
	    }else if("deletecomment".equals(method)){//删除comment
	    	System.out.println("member....deletecomment....");
	    	int id=Integer.valueOf(request.getParameter("id"));
	    	UserDao ud=new UserImpl();
	    	ud.deleteComment(id);
	    	JSONObject json=new JSONObject();
	    	json.element("success", true);
	    	PrintWriter out=response.getWriter();
	    	out.write(json.toString());
	    	System.out.println(json.toString());
	    	return;
	    	
	    }else if("editcomment".equals(method)){//修改comment
	    	System.out.println("member....editcomment.....");
	    	int id =Integer.valueOf(request.getParameter("id"));
	    	String content=request.getParameter("content");
	    	System.out.println(id);
	    	System.out.println(content);
	    	UserDao ud=new UserImpl();
	    	ud.editComment(id, content);
	    	JSONObject json=new JSONObject();
	    	json.element("success", true);
	    	PrintWriter out=response.getWriter();
	    	out.write(json.toString());
	    	System.out.println(json.toString());
	    	return;
	    	
	    }else if("timermessage".equals(method)){//定时刷新消息弹出框
	    	/*System.out.println("member....timermessage...");
	    	String assistid=((Assister)request.getSession().getAttribute("assist")).getId();
	    	System.out.println(assistid);
	    	UserDao ud=new UserImpl();
	        ArrayList<TimerMessage> list=ud.findTimerMessage(assistid);
	    	JSONObject json=new JSONObject();
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(java.util.Date.class,new JsonValueProcessor() {
			  private SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
			  public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
			    return  value == null ?"" : sd.format(value);
			  }
			  public Object processArrayValue(Object value, JsonConfig jsonConfig) {
			    return null;
			  }
			});
			System.out.println(list);
			if(list!=null){//保证只弹出一次
				for(TimerMessage tm:list){
					ud.closeTimerAfterShow(tm.getId());
				}
				json.element("success", true);
				json.element("messages", JSONArray.fromObject(list, jsonConfig));
			}else{
				json.element("success", false);
			}
			PrintWriter out=response.getWriter();
	    	out.write(json.toString());
	    	System.out.println(json.toString());
	    	return;*/
	    	
	    }else if("userinfo".equals(method)){//手机端用户信息by uid
	    	String uid=request.getParameter("uid");
	    	UserDao ud=new UserImpl();
	    	User user=ud.findByUid(uid);
    	    JSONObject json = new JSONObject();
		    json.element("user",user);
		    response.setContentType("application/x-json;charset=utf-8");
			PrintWriter out=response.getWriter();
			out.write(json.toString());
		    return;
	    	
	    }else if("asssitinfo".equals(method)){//手机端用户助理
	    	System.out.println("member....assistinfo....");
	    	String uid=request.getParameter("uid");
	    	AssistDao ad=new AssistImpl();
	    	Assister assist=ad.getAssistByUid(uid);
	    	JSONObject json = new JSONObject();
		    json.element("assist",assist);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
		    return;
	    }else if("edituser".equals(method)){//手机端，修改个人资料
	    	System.out.println("member....edituser....");
	    	String uid=request.getParameter("uid");
	    	String phone=request.getParameter("phone");
	    	String email=request.getParameter("email");
	    	String school=request.getParameter("school");
	    	String grade=request.getParameter("grade");
	    	String major=request.getParameter("major");
	    	String address=request.getParameter("address");
	    	String name=request.getParameter("name");
	    	User user=new User();
	    	user.setUid(uid);
	    	user.setPhone(phone);
	    	user.setEmail(email);
	    	user.setSchool(school);
	    	user.setGrade(Integer.valueOf(grade));
	    	user.setName(name);
	    	user.setMajor(major);
	    	user.setAddress(address);
	    	UserDao ud=new UserImpl();
	    	ud.updateUserInfo(user);
	    	JSONObject json = new JSONObject();
		    json.element("ok",true);
			PrintWriter out=response.getWriter();
			out.write(json.toString());
		    return;
	    }
	    else if("mobile".equals(method)){//手机端
			String page=request.getParameter("page");
			if("add".equals(page)){//加入会员按钮
				System.out.println("add........");
				String name=request.getParameter("name");
				String phone=request.getParameter("phone");
				String email=request.getParameter("email");
				String school=request.getParameter("school");
				String grade=request.getParameter("grade");
				System.out.println(name);
				System.out.println(phone);
				System.out.println(email);
				System.out.println(school);
				System.out.println(grade);
				User user=new User();
				user.setPhone(phone);
				user.setName(name);
				user.setEmail(email);
				user.setSchool(school);
				user.setGrade(Integer.parseInt(grade));
				UserDao userd=new UserImpl();
				userd.update2(user);
				System.out.println("成功.......");
			    JSONObject json = new JSONObject();
			    json.element("msg","加入会员成功");
			    response.setContentType("application/x-json;charset=utf-8");
				PrintWriter out=response.getWriter();
				out.write(json.toString());
			    return;
			}else if("editpage".equals(page)){//请求个人资料编辑页面
				String uid=request.getParameter("uid");
				System.out.println("uid......."+uid);
				UserDao userd=new UserImpl();
				User user=userd.findByUid(uid);
				request.setAttribute("user", user);
				request.getRequestDispatcher("/WEB-INF/page/user/useredit.jsp").forward(request, response);
			}else if("edit".equals(page)){//保存资料更新
				System.out.println("add........");
				String name=request.getParameter("name");
				String phone=request.getParameter("phone");
				String email=request.getParameter("email");
				String school=request.getParameter("school");
				String grade=request.getParameter("grade");
				System.out.println(name);
				System.out.println(phone);
				System.out.println(email);
				System.out.println(school);
				System.out.println(grade);
				User user=new User();
				user.setPhone(phone);
				user.setName(name);
				user.setEmail(email);
				user.setSchool(school);
				user.setGrade(Integer.parseInt(grade));
				UserDao userd=new UserImpl();
				userd.update2(user);
				System.out.println("成功.......");
			    JSONObject json = new JSONObject();
			    response.setContentType("application/x-json;charset=utf-8");
				PrintWriter out=response.getWriter();
				out.write(json.toString());
			}else if("join".equals(page)){//申请加入菜单
				System.out.println("join...........");
				String code=request.getParameter("code");	
				String key=request.getParameter("key");
				System.out.println("key"+key);
				String openid=null;
				String access_token=null;
				//查询openid是否已经绑定
			    if(key==null){
			    	//获取openid
					if(code!=null){
						System.out.println("menu join.....");
						//通过code换取access_token
						String urlApi="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxf34da76143f5050d&secret=7a6df503a9afaa70601d652cfb99b16d&code=CODE&grant_type=authorization_code";
						String url=urlApi.replace("CODE", code);
						try {
							String str=CommonUtil.httpsRequest(url, "POST", null);
						   //json-lib解析json字符串
							JSONObject json=JSONObject.fromObject(str);
							if(null!=json){
								access_token=json.getString("access_token");
								openid= json.getString("openid");
								System.out.println("openid..."+openid);
							}
						} catch (Exception e) {
							e.printStackTrace();
						}
				    }
					
			    }else if("center".equals(key)){
			    	openid=request.getAttribute("openid").toString();
			    	
			    }else if("pay".equals(key)){
			    	openid=request.getAttribute("openid").toString();
			    }
				 UserDao userdao=new UserImpl();
				 if(userdao.findByOpenId(openid)==null){
					 request.setAttribute("key", "1");//1.表示会弹出模态框进行绑定
				 }else{
					 System.out.println("2............");
					 request.setAttribute("key", "2");//直接填写资料加入会员
				 }
				 request.setAttribute("openid", openid);
				request.getRequestDispatcher("/WEB-INF/page/user/applyform.jsp").forward(request, response);
			}else if("center".equals(page)){//个人中心菜单
				System.out.println("center............");
				String openid=null;
				String access_token=null;
				//查询openid是否已经绑定
				String code=request.getParameter("code");	
				//获取openid
				if(code!=null){
					//通过code换取access_token
					String urlApi="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxf34da76143f5050d&secret=7a6df503a9afaa70601d652cfb99b16d&code=CODE&grant_type=authorization_code";
					String url=urlApi.replace("CODE", code);
					try {
						String str=CommonUtil.httpsRequest(url, "POST", null);
					 //解析json字符串
						JSONObject json=JSONObject.fromObject(str);
						if(null!=json){
							access_token= json.getString("access_token");
							openid=json.getString("openid");
							System.out.println("openid..."+openid);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
			    }
				 UserDao userdao=new UserImpl();
				 User user=userdao.findByOpenId(openid);
				 if(user!=null&&user.getType()!=0){//说明已经加入了会员
					 request.setAttribute("uid", user.getUid());
					 request.getRequestDispatcher("/WEB-INF/page/user/usercenter.jsp").forward(request, response);
				 }else{
					 request.setAttribute("openid", openid);
					 request.getRequestDispatcher("member?method=mobile&page=join&key=center").forward(request, response);//返回到加入会员菜单对应的url
				 }
			}else if("pay".equals(page)){//在线支付菜单
				System.out.println("pay............");
				String openid=null;
				String access_token=null;
				//查询openid是否已经绑定
				String code=request.getParameter("code");	
				//获取openid
				if(code!=null){
					//通过code换取access_token
					String urlApi="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxf34da76143f5050d&secret=7a6df503a9afaa70601d652cfb99b16d&code=CODE&grant_type=authorization_code";
					String url=urlApi.replace("CODE", code);
					try {
						String str=CommonUtil.httpsRequest(url, "POST", null);
					 //解析json字符串
						JSONObject json=JSONObject.fromObject(str);
						if(null!=json){
							access_token=json.getString("access_token");
							openid=json.getString("openid");
							System.out.println("openid..."+openid);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
			    }
				 UserDao userdao=new UserImpl();
				 User user=userdao.findByOpenId(openid);
				 if(user!=null&&user.getType()!=0){//说明已经加入了会员,跳转到支付界面
					 request.setAttribute("uid", user.getUid());
					 request.getRequestDispatcher("/WEB-INF/page/user/pay.jsp").forward(request, response);
				 }else{//跳转到申请加入菜单对应的url
					 request.setAttribute("openid", openid);
					 request.getRequestDispatcher("member?method=mobile&page=join&key=pay").forward(request, response);//返回到加入会员菜单对应的url
				 }
			}
		}
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
